package com.teamelite.exmgmt;

import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

@SuppressLint("SimpleDateFormat")
public class cActivityDoDone extends Activity implements ViewFactory,View.OnClickListener {
	ArrayList<arrList> lstPengguna,lstTips,lstActivity;
	int tips_count,last_tipsid;
	String hr,min,sec,id_pengguna,id_activity,datestart,duration,resttime,r_hr,r_min,r_sec;
	TextSwitcher txtInfo;
	Animation in,out;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_doactivitydone);
		ContentValues values=new ContentValues();
		DBHelper db = new DBHelper(this);
		
		
		this.tips_count = db.getCountTips();
		this.last_tipsid = -1;
		
		this.hr = getIntent().getExtras().getString("hr");
		this.min = getIntent().getExtras().getString("min");
		this.sec = getIntent().getExtras().getString("sec");
		this.r_hr = getIntent().getExtras().getString("r_hr");
		this.r_min = getIntent().getExtras().getString("r_min");
		this.r_sec = getIntent().getExtras().getString("r_sec");
		this.id_pengguna = getIntent().getExtras().getString("id_pengguna");
		this.datestart = getIntent().getExtras().getString("datestart");
		this.duration= String.format("%02d:%02d:%02d", Integer.parseInt(this.hr),Integer.parseInt(this.min),Integer.parseInt(this.sec));
		
		long tmp_hr,tmp_min,tmp_sec;
		tmp_sec = Integer.parseInt(this.r_sec);
		tmp_min = (Integer.parseInt(this.r_min)+(tmp_sec/60));
		tmp_hr = (Integer.parseInt(this.r_hr)+(tmp_min/60));
		this.resttime=String.format("%02d:%02d:%02d", tmp_hr,tmp_min%60,tmp_sec%60);
		
		this.id_activity = getIntent().getExtras().getString("id_activity");
		double mintotal = ((((Double.parseDouble(this.hr)*60)+Double.parseDouble(this.min))*60)+Double.parseDouble(this.sec))/60;
		
		lstPengguna = db.getPengguna(this.id_pengguna);
		lstTips = db.getTips();
		lstActivity = db.getCatActivity(this.id_activity);

		double t_calories = mintotal * (db.getMet(this.id_activity) * 3.5 * Double.parseDouble(lstPengguna.get(0).berat))/200.0;
		values.put("idp", this.id_pengguna);
		values.put("date", this.datestart);
		values.put("id_activity", this.id_activity);
		values.put("durationtodo", this.duration);
		values.put("resttime", this.resttime);
		values.put("calory", t_calories);
		
		// Set Ke Overview
		((TextView)findViewById(R.id.txtActivityName)).setText(""+lstActivity.get(0).nama_activity);
		((TextView)findViewById(R.id.txtDuration)).setText(""+this.duration);
		((TextView)findViewById(R.id.txtRestDuration)).setText(""+this.resttime);
		((TextView)findViewById(R.id.txtCalories)).setText(String.format("%.2f cal", t_calories));
		
		// Set Listener Tombol
		((ImageView)findViewById(R.id.btnBack)).setOnClickListener(this);
		((ImageView)findViewById(R.id.btnCancelR)).setOnClickListener(this);
		
		txtInfo = (TextSwitcher) findViewById(R.id.txtInfo);
		txtInfo.setFactory(cActivityDoDone.this);
        in = AnimationUtils.loadAnimation(cActivityDoDone.this, R.anim.push_up_in);
        out = AnimationUtils.loadAnimation(cActivityDoDone.this, R.anim.push_up_out);
        
		Timer timer = new Timer();
		timer.schedule(new tTask(), 0,10000);
		db.addActivityData(values);
	}
	
	class tTask extends TimerTask {
	     @Override
	     public void run() {
	         cActivityDoDone.this.runOnUiThread(new Runnable() {
	             @Override
	             public void run() {
	            	 Random generate = new Random();
	            	 int r;
	                 txtInfo.setInAnimation(in);
	                 txtInfo.setOutAnimation(out);
	                 do{
	                	 r = generate.nextInt(tips_count);
	                 }while(r==last_tipsid);
	                 last_tipsid = r;
	                 txtInfo.setText(lstTips.get(r).tips);
	             }
	         });
	     }
	}

	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		TextView t = new TextView(this);
        t.setGravity(Gravity.TOP | Gravity.LEFT);
        t.setTextSize(14);
        return t;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
			case R.id.btnBack:
				finish();
			break;
			case R.id.btnCancelR:
				DBHelper db = new DBHelper(this);
				db.delCancelActivity(this.id_pengguna, this.id_activity, this.datestart);
				finish();
			break;
		}
	};

}
